require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')
require('../libs/iconfont/iconfont.css')
require('../css/login.less')

const { default: axios } = require('axios')

//weui.css
require('weui')

//weui.js
const weui = require('weui.js')


require('../css/login.less')
document.addEventListener('DOMContentLoaded', function () {
    const phoneNum = document.querySelector('#phoneNum')
    const pwd = document.querySelector('#pwd')
    let verObj = [phoneNum, pwd]


    function rmBorder(obj) {
        obj.forEach(function (dom) {
            dom.style.border = 'none'
        })
    }


    subBtn.addEventListener('click', function () {
        const phoneNumVal = phoneNum.value
        const pwdVal = pwd.value

        if (!/^1[3-9][0-9]{9}$/.test(phoneNumVal)) {
            rmBorder(verObj)
            phoneNum.style.border = '1px solid red'
        } else if (!/^[a-zA-Z]\w{5,10}$/.test(pwdVal)) {
            rmBorder(verObj)
            pwd.style.border = '1px solid red'
        } else {
            rmBorder(verObj);
            //提交数据
            axios
                .post("http://139.9.177.51:8099/users/login", {
                    account: phoneNumVal,
                    password: pwdVal,
                })
                .then(function (res) {
                    console.log(res.data);
                    if (res.data.status === 0) {
                        weui.toast('登录成功', {
                            duration: 1000,
                            className: 'custom-classname',
                            callback: function () {
                                location.href = "./home.html";
                                console.log(res);
                                localStorage.setItem('userId',res.data.data.user.userId)
                            }
                        });
                    } else {
                        weui.toast(res.data.msg, {
                            duration: 1000,
                            className: 'custom-classname',
                        })
                    }
                });
        }
    });

})